let a;
let b;
let c;
let d;
// a: 表示按id进行模糊查询
// b: 表示按真实姓名进行模糊查询
// c: 表示按登录的名字进行模糊查询
// d: 表示按照状态进行查询
console.log("a ===" + a + "-----> b==" + b + "----> c==" + c + "-----> d=" + d);
$(function () {

    // let id_search = $('input[name="id_search"]').val();
    // let name_search = $('input[name="name_search"]').val();
    // let username_search = $('input[name="username_search"]').val();
    // let state_search = $('#state_search').val();

    var currentPage = 1;
    var count = 4;
    var pages;

    a = $('input[name="id_search"]').val();
    b = $('input[name="name_search"]').val();
    c = $('input[name="username_search"]').val();
    d = $('#state_search').val();

    getRemoteAccount(currentPage, count, a, b, c, d)

    function handlerRespone(resp) {
        if (resp.status === 200) {

            var totalCount = resp.data.totalCount;
            pages = Math.ceil(totalCount / count)

            drawPageIndicator(pages)

            var items = resp.data.items;
            console.log(items)
            putItemsInTable(items)
        }
    }


    function getRemoteAccount(page, count, id_search, name_search, username_search, state_search) {
        page = +page <= 0 ? 1 : +page;
        count = +count <= 0 ? 4 : +count;

        let url = '/account/findAllAccount';
        var params = {
            page: page,
            count: count,
            idSearch: id_search,
            nameSearch: name_search,
            usernameSearch: username_search,
            stateSearch: state_search
        }
        $.get(url, params, handlerRespone)
    }

    function drawPageIndicator(pages) {
        var ul = $('#ul')
        ul.empty()
        for (var i = 1; i <= pages; i++) {
            var li = $('<li class="page_li" style="float: left"></li>')

            var a = $('<a></a>');
            // 给a标签添加属性,这条属性的意思为点击链接后不进行页面上的跳转.
            a.attr('href', 'javascript:void(0)');
            a.text(i);


            if (currentPage == i) {
                a.addClass('current_page')
            }

            a.click(onIndicatorClick)

            li.append(a);
            ul.append(li);

        }
    }

    function onIndicatorClick() {
        currentPage = +$(this).text();
        console.log($(this))

        a = $('input[name="id_search"]').val();
        b = $('input[name="name_search"]').val();
        c = $('input[name="username_search"]').val();
        d = $('#state_search').val();

        getRemoteAccount(currentPage, count, a, b, c, d)

    }

    $('#firstPage').click(function () {
        currentPage = 1;
        a = $('input[name="id_search"]').val();
        b = $('input[name="name_search"]').val();
        c = $('input[name="username_search"]').val();
        d = $('#state_search').val();
        getRemoteAccount(currentPage, count, a, b, c, d)

    })

    $('#prev').click(function () {
        --currentPage;
        if (currentPage < 1) {
            currentPage = 1
        }
        a = $('input[name="id_search"]').val();
        b = $('input[name="name_search"]').val();
        c = $('input[name="username_search"]').val();
        d = $('#state_search').val();
        getRemoteAccount(currentPage, count, a, b, c, d)
    })

    $('#next').click(function () {
        ++currentPage;
        if (currentPage > pages) {
            currentPage = 1
        }
        a = $('input[name="id_search"]').val();
        b = $('input[name="name_search"]').val();
        c = $('input[name="username_search"]').val();
        d = $('#state_search').val();
        getRemoteAccount(currentPage, count, a, b, c, d)
    })

    $('#lastPage').click(function () {
        currentPage = pages;
        a = $('input[name="id_search"]').val();
        b = $('input[name="name_search"]').val();
        c = $('input[name="username_search"]').val();
        d = $('#state_search').val();
        getRemoteAccount(currentPage, count, a, b, c, d)
    })


    function putItemsInTable(items) {
        var tbody = $('#tbody')
        tbody.empty();

        for (var i of items) {
            let tr = $('<tr></tr>')
            let td_id = $('<td></td>').html(i.accountId)
            let td_name = $('<td></td>')
            let a = $('<a href="/account/account_detail.html" class="real_name"></a>').html(i.realName)
            td_name.append(a)

            let td_card = $('<td></td>').html(i.idcardNo)
            let user_name = $('<td></td>').html(i.loginName)
            let status = i.accountStatus;
            let state;
            if (status == 1) {
                state = '开通'
            }
            if (status == 2) {
                state = '暂停'
            }
            if (status == 3) {
                state = '删除'
            }
            let td_state = $('<td></td>').html(state)
            let td_create = $('<td></td>').html(i.createDate)
            let last_login_time = $('<td></td>').html(i.lastLoginTime)
            let td_modi = $('<td></td>');

            if (status == 2) {
                let b_start = $('<input type="button" value="开通" class="btn_start" />')
                let b_pause = $('<input type="button" value="暂停" class="btn_pause" style="display: none"/>')
                let b_modify = $('<input type="button" value="修改" class="btn_modify" />')
                let b_delete = $('<input type="button" value="删除" class="btn_delete" />')
                td_modi.append(b_start).append(b_pause).append(b_modify).append(b_delete)
            }
            if (status == 1) {
                let b_start = $('<input type="button" value="开通" class="btn_start" style="display: none" />')
                let b_pause = $('<input type="button" value="暂停" class="btn_pause" />')
                let b_modify = $('<input type="button" value="修改" class="btn_modify" />')
                let b_delete = $('<input type="button" value="删除" class="btn_delete" />')
                td_modi.append(b_start).append(b_pause).append(b_modify).append(b_delete)
            }


            tr.append(td_id).append(td_name).append(td_card).append(user_name).append(td_state).append(td_create)
                .append(last_login_time).append(td_modi);
            tbody.append(tr)
        }

    }

    var body = $('body');


    body.on('click', '.btn_search', function () {

        let id_search = $('input[name="id_search"]').val();
        let name_search = $('input[name="name_search"]').val();
        let username_search = $('input[name="username_search"]').val();
        let state_search = $('#state_search').val();

        getRemoteAccount(currentPage, count, id_search, name_search, username_search, state_search)
    })


    body.on('click', '.real_name', function () {
        var account_id = $(this).parents("tr").children().eq(0).html();
        window.localStorage.setItem("accountId", account_id)

        window.location.href = '/account/account_detail.html';

    })


    body.on('click', '.btn_modify', function () {
        var account_id = $(this).parents("tr").children().eq(0).html();
        window.localStorage.setItem("accountId", account_id)

        window.location.href = '/account/account_modi.html'
    })

    body.on('click', '.btn_start', function () {
        $(this).css('display', 'none');
        $(this).next().css('display', 'inline');

        $(this).hidden
        $(this).next().show()

        window.location.reload()

        var account_id = $(this).parents("tr").children().eq(0).html();
        updStatus(account_id, 1)


    })

    body.on('click', '.btn_pause', function () {
        var account_id = $(this).parents("tr").children().eq(0).html();
        $(this).css('display', 'none');
        $(this).prev().css('display', 'inline');

        window.location.reload()


        updStatus(account_id, 2)

    })

    body.on('click', '.btn_delete', function () {
        var account_id = $(this).parents("tr").children().eq(0).html();

        window.location.reload()

        $(this).parents("td").empty();

        updStatus(account_id, 3)

    })


    function updStatus(account_id, status) {
        let url = '/account/updStatus';
        var params = {
            accountId: account_id,
            status: status
        }
        $.post(url, params)
    }


})


